package project.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Users.Client;
import schronisko.Queries;

/**
 * Servlet implementation class User_functions
 */
@WebServlet("/User_functions")
public class User_functions extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	HttpSession s;
	Queries q = new Queries();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public User_functions() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		
		String function = (String)s.getAttribute("function"); // ktora funkcja wywolana
		
		int ID = (Integer)s.getAttribute("ID");
		switch(function){
		case "change_pass":
			String old_pass = request.getParameter("old_pass");
			String new_pass = request.getParameter("new_pass");
			String new_pass1 = request.getParameter("new_pass1");
			if(!new_pass.equals(new_pass1)) {
				s.setAttribute("changed_pass", 2);
			}
			else {
				int rows = q.change_pass(old_pass, new_pass, new_pass1, ID);
				if(rows == 0){ // zmieniono
					s.setAttribute("changed_pass", 1);
				}
				else if(rows == -2) { // hasla sie nie zgadzaja
					s.setAttribute("changed_pass", 2);
				}
				else if(rows == -1) { // problem z baza
					s.setAttribute("changed_pass", 3);
				}
			}
			response.sendRedirect("user/change_pass.jsp");
			break;
		case "edit_profile":
			Client c = null;
			c = (Client)s.getAttribute("client_data");
			String name = (String)request.getParameter("name");
			String surname = (String)request.getParameter("surname");
			String post = (String)request.getParameter("post");
			String city = (String)request.getParameter("city");
			String house = (String)request.getParameter("house");
			String pesel = (String)request.getParameter("pesel");
			String id_card = (String)request.getParameter("id_card");
			
			if(!name.equals(""))
				c.setName(name);
			if(!surname.equals(""))
				c.setSurname(surname);
			if(!city.equals(""))
				c.setCity(city);
			if(!house.equals(""))
				c.setHouse(house);
			if(!post.equals(""))
				c.setPost(post);
			if(!pesel.equals(""))
				c.setPESEL(pesel);
			if(!id_card.equals(""))
				c.setID_card(id_card);
			
			int rows = q.updateClient(c);
			if(rows == 0) {
				s.setAttribute("edited", 1);
			}
			else if(rows == -1) {
				s.setAttribute("edited", 2);

			}
			response.sendRedirect("user/edit.jsp");
			break;
		case "add_animal":
			ID = Integer.parseInt(request.getParameter("id"));
			q.deleteBook(ID);
			s.setAttribute("adding", 0);
			response.sendRedirect("user/add.jsp");
			break;
		}

	}

}
